/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chanceit.ssss.activity;

import org.gl.customview.ViewPager;
import org.gl.utils.Tip;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.chanceit.ssss.adapters.CarerPagerAdapter;
import com.chanceit.ssss.common.IntentExtraDataKey;
import com.chanceit.ssss.fragments.BaseFragment;
import com.chanceit.ssss.fragments.CarerInfoFragment;
import com.chanceit.ssss.fragments.CarerListFragment.OnListFragmentSelectedListener;

public class CarerActivity extends BaseFragmentActivity implements
		OnListFragmentSelectedListener, PageViewHandlerI {
	CarerPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_carer);
		mSectionsPagerAdapter = new CarerPagerAdapter(
				getSupportFragmentManager(), this);

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		// int selectpage =
		// GlobalVariable.getGlobalInstance().intent.getIntExtra(IntentExtraDataKey.CARER_SELECT_PAGE,
		// 0);
		// GlobalVariable.getGlobalInstance().intent.putExtra(IntentExtraDataKey.CARER_SELECT_PAGE,
		// 0);//清楚点这个状态
		// switch (selectpage) {
		// case IntentExtraDataKey.CARER_SELECT_PAGE_UPDATE:
		// switchViewPage(1);
		// break;
		// default:
		// break;
		// }
	}

	public void onArticleSelected(int position) {
		Toast.makeText(getApplicationContext(), "no response" + position,
				Toast.LENGTH_SHORT).show();
		// startActivity(GlobalVariable.getGlobalInstance().intent.setAction(ActivityActionName.CARER));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case IntentExtraDataKey.SCAN:
			if (resultCode == RESULT_OK && data != null) {
				// intent.putExtra("SCAN_RESULT", rawResult.toString());
				// intent.putExtra("SCAN_FORMATS", rawResult
				// .getBarcodeFormat().toString());
				mSectionsPagerAdapter.sendMessage(0,
						data.getStringExtra("SCAN_RESULT"));
			}

			break;
		case CarerInfoFragment.RequestCode_PickCarType:
			if (resultCode == RESULT_OK && data != null) {
				Fragment fragment = mSectionsPagerAdapter.getFagments()[mViewPager
						.getCurrentItem()];
				if (fragment instanceof CarerInfoFragment) {
					((CarerInfoFragment) fragment)
							.updateCarType(
									data.getIntExtra(
											CarTypePickerActivity.key_PickedCarID,
											0),
									data.getStringExtra(CarTypePickerActivity.key_PickName));
				}
			}

			break;

		default:
			// intent.putExtra("SCAN_RESULT", rawResult.toString());
			// intent.putExtra("SCAN_FORMATS", rawResult
			// .getBarcodeFormat().toString());
			mSectionsPagerAdapter.sendMessage(0,
					data.getStringExtra("SCAN_RESULT"));
			break;
		}
	}

	private boolean isCanFinishActivity() {
		if (mViewPager.getCurrentItem() != 0) {
			mViewPager.setCurrentItem(0);
			return false;
		} else {
			Fragment mFragment = mSectionsPagerAdapter.getFagments()[mViewPager
					.getCurrentItem()];
			if (mFragment != null) {
				if (mFragment instanceof BaseFragment) {
					return !((BaseFragment) mFragment).onBackPressed();
				}
			}
			return true;
		}
	}

	@Override
	public void onBackPressed() {
		if (isCanFinishActivity()) {
			super.onBackPressed();
		} else {
		}
	}

	@Override
	public void switchViewPage(int postion) {
		if (postion < mSectionsPagerAdapter.getCount() && postion >= 0) {
			mViewPager.setCurrentItem(postion);
		} else {
			Tip.show("the postion out of viewPage count");
		}

	}
}